package kara;



import java.io.*;
//代码随想录p101
public class A102 {
    static int n;
    static int m;
    static int[][] map;
    static int res=0;
    static int[] dir1={1,0,-1,0};
    static int[] dir2={0,1,0,-1};
    static int[][] vis;
    static int ans;
    static int go;
    static BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter log=new BufferedWriter(new OutputStreamWriter(System.out));
    public static void main(String[] args) throws IOException {
        String[] split = read.readLine().split(" ");
        n=Integer.parseInt(split[0]);
        m=Integer.parseInt(split[1]);
        map=new int[n][m];
        vis=new int[n][m];
        for(int i=0;i<n;i++){
            String[] s = read.readLine().split(" ");
            for(int j=0;j<m;j++){
                map[i][j]=Integer.parseInt(s[j]);
            }
        }
        //处理边界的岛
        for(int i=0;i<n;i++){
            if(map[i][0]==1)dfs(i,0);
            if(map[i][m-1]==1)dfs(i,m-1);
        }
        for(int j=0;j<m;j++){
            //将边缘岛屿标记为2
            if(map[0][j]==1)dfs(0,j);
            if(map[n-1][j]==1)dfs(n-1,j);
        }
        //将2为1输出
        //将1为0输出
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (map[i][j] == 2) {
                    log.write("1");
                } else if (map[i][j] == 1) {
                    log.write("0");
                } else {
                    log.write(Integer.toString(map[i][j]));
                }
                if (j < m - 1) {
                    log.write(" ");
                }
            }
            log.newLine();
        }
        log.flush();
    }

    //dfs处理某个岛屿所有陆地
    static void dfs(int x,int y){
        if(vis[x][y]==1||map[x][y]==0)return;//被处理或者是海洋就退出
        vis[x][y]=1;//标记
        map[x][y]=2;//处理
        for(int i=0;i<4;i++) {
            int a = x + dir1[i];
            int b = y + dir2[i];
            if (a >= 0 && a < n && b >= 0 && b < m) {
                dfs(a,b);
            }
        }
    }
}
